package com.eduhzy.ttw.work.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.work.R;
import com.eduhzy.ttw.work.R2;
import com.eduhzy.ttw.work.di.component.DaggerWorkStatusComponent;
import com.eduhzy.ttw.work.di.module.WorkStatusModule;
import com.eduhzy.ttw.work.mvp.contract.WorkStatusContract;
import com.eduhzy.ttw.work.mvp.model.entity.WorkClassData;
import com.eduhzy.ttw.work.mvp.model.entity.WorkDetailData;
import com.eduhzy.ttw.work.mvp.presenter.WorkStatusPresenter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.Subscriber;

import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-12-3 16:43:36
 */
@Route(path = RouterHub.WORK_WORKSTATUSFRAGMENT)
public class WorkStatusFragment extends BaseFragment<WorkStatusPresenter> implements WorkStatusContract.View {

    @BindView(R2.id.rv_list)
    RecyclerView mRvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    private HomeFragmentAdapter mFragmentAdapter;
    private WorkDetailData.HomeworkClassListBean mBean;
    @Inject
    BaseQuickAdapter<WorkClassData, AutoBaseViewHolder> mAdapter;
    @Inject
    RecyclerView.LayoutManager mLayoutManager;
    private String title;

    public static WorkStatusFragment newInstance(WorkDetailData.HomeworkClassListBean bean, int type) {
        WorkStatusFragment fragment = new WorkStatusFragment();
        WorkDetailData.HomeworkClassListBean newBean = null;
        try {
            newBean = (WorkDetailData.HomeworkClassListBean) bean.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }


        newBean.setType(type);
        fragment.setData(newBean);
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerWorkStatusComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .workStatusModule(new WorkStatusModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.work_fragment_work_status, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        getData();
        mRvList.setLayoutManager(mLayoutManager);
        mRvList.setAdapter(mAdapter);
        mSwipeLayout.setOnRefreshListener(() -> getData());
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            WorkClassData workClassData = mAdapter.getData().get(position);
            mBean.setStudentId(workClassData.getUserId());
            if (mBean.getType() == 3) {
                ARouter.getInstance().build(RouterHub.WORK_CHECKWORKACTIVITY)
                        .withString(Intent.EXTRA_TITLE, "批改作业")
                        .withInt(Constants.ROUTER_INTEGER, 1)
                        .withParcelable(Constants.PARCELABLE_DATA, mBean)
                        .navigation(getActivity());
            } else if (mBean.getType() == 4) {
                ARouter.getInstance().build(RouterHub.WORK_CHECKWORKACTIVITY)
                        .withString(Intent.EXTRA_TITLE, "已批改作业")
                        .withInt(Constants.ROUTER_INTEGER, 2)
                        .withParcelable(Constants.PARCELABLE_DATA, mBean)
                        .navigation(getActivity());
            }
        });
    }

    private void getData() {
        mPresenter.workRecordList4Teacher(mBean.getWorkId(), mBean.getClassId(), mBean.getType());
    }

     @Subscriber(tag = RouterHub.WORK_WORKSTATUSFRAGMENT)
     public void onEvents(Message msg) {
         switch (msg.what) {
             case EventBusTags.CHECK_COMPLETE:
                 getData();
                 break;
                 default:
         }
     }


    @Override
    public void setData(@Nullable Object data) {
        this.mBean = (WorkDetailData.HomeworkClassListBean) data;
    }

    @Override
    public void showLoading() {
        mSwipeLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeLayout.setRefreshing(false);

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    public void setPageAdapter(HomeFragmentAdapter adapter) {
        this.mFragmentAdapter = adapter;
    }

    //更新ViewPager的Title信息
    @Override
    public void updatePagerTitle(int count) {
        if (mFragmentAdapter != null) {
            for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
                if (mFragmentAdapter.getItem(i) == WorkStatusFragment.this) {
                    if (title == null) {
                        title = mFragmentAdapter.getPageTitle(i).toString();
                    }
                    mFragmentAdapter.setPageTitle(i, String.format(Locale.CHINESE, "%s  %s", title, count));
                    break;
                }
            }
        }
    }
}
